﻿using System;
using System.Drawing;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Collections;
namespace DoAnGiaPhaVersion1._0
{
    public partial class Form1 : Form
    {
        ThanhVienBUS _thanhvien;
        ArrayList _dsthanhvien;
        string[] showColumn = {"TenThanhVien","NamSinh","NamMat","Tuoi" };
        string[] headerText = { "Tên thành viên", "Năm sinh", "Năm mất", "Tuổi"};
        public Form1()
        {
            InitializeComponent();
            panel_detail.Visible = false;
            uS_ChiTietThanhVien1.DuongDanHinh_DoubleClick += new US_ChiTietThanhVien.DuongDanHinh_DoubleClickHandler(uS_ChiTietThanhVien1_DuongDanHinh_DoubleClick);
            uS_ThongTinThanhVien1.ChiTiet_Click += new US_ThongTinThanhVien.ChiTiet_ClickHandler(ChiTietThanhVien);
            uS_ChiTietThanhVien1.Xong_Click +=new US_ChiTietThanhVien.Xong_ClickHandler(update);
            uS_ThongTinThanhVien1.HinhDaiDien_Click += new US_ThongTinThanhVien.HinhDaiDien_ClickHandler(uS_ThongTinThanhVien1_HinhDaiDien_Click);
            string[] _chuoiChucNang = {"Cha-Mẹ", "Anh-Em Trai", "Chị-Em Gái", "Vợ-Chồng", "Con Cái" };
            ComboboxItem[] _item = new ComboboxItem[_chuoiChucNang.Length];            
            for (int i = 0; i < _chuoiChucNang.Length; i++)
            {
                _item[i] = new ComboboxItem();
                _item[i].Text = _chuoiChucNang[i];
                cb_ChucNang.Items.Add(_item[i]);    
            }
            cb_ChucNang.SelectedIndex = 0;
            
        }

        void uS_ThongTinThanhVien1_HinhDaiDien_Click()
        {
            panel_first.Visible = false;
            panel_detail.Visible = true;
        }

        private void uS_ChiTietThanhVien1_DuongDanHinh_DoubleClick()
        {
            string path = UtilitiesGUI.openFile();
            uS_ChiTietThanhVien1.HinhDaiDien = Image.FromFile(path);
            UtilitiesGUI.copyFile(path,1);
        }
        private void update()
        {
            _thanhvien.update();
            panel_first.Visible = true;
            panel_detail.Visible = false;
        }
        private void ChiTietThanhVien()
        {
            panel_first.Visible = false;
            panel_detail.Visible = true;
        }
        private void HinhDaiDienVaTieuSu()
        {
            MessageBox.Show("Click hinh dai dien");
        }
        private void reLoadData()
        {
            _dsthanhvien = _thanhvien.getDsThanhVien();
            this.lbl_soLuongThanhVien.Text = "( "+_dsthanhvien.Count.ToString()+" )";
            dataGrid_ThanhVien.DataSource = _dsthanhvien;
            UtilitiesGUI.showColumn(dataGrid_ThanhVien, showColumn, headerText);
        }
        private void Form1_Load(object sender, EventArgs e)
        {                      
            _thanhvien = new ThanhVienBUS();
            reLoadData();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            //string tenthanhvien = txt_hoTen.Text;
            //int gioitinh = 0;
            //if (uS_GioiTinh1.Nam)
            //{
            //    gioitinh = 1;
            //}
            //_dsthanhvien = _thanhvien.findDanhSachThanhVien(tenthanhvien, gioitinh);
            //dataGrid_ThanhVien.DataSource = _dsthanhvien;
        }

        private void dataGrid_ThanhVien_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                lbl_HoTen.Text = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["TenThanhVien"].Value.ToString();
                uS_ThongTinThanhVien1.HoTen = lbl_HoTen.Text;
                uS_ThongTinThanhVien1.NgaySinh = DateTime.Parse(dataGrid_ThanhVien.Rows[e.RowIndex].Cells["NgaySinh"].Value.ToString()).ToShortDateString();
                uS_ThongTinThanhVien1.QueQuan = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["QueQuan"].Value.ToString();
                string _duongdanhinhdaidien = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["DuongDanHinh"].Value.ToString();
                if (_duongdanhinhdaidien != "")
                {
                    Bitmap _bmp = new Bitmap(_duongdanhinhdaidien);
                    if (_bmp != null)
                    {
                        uS_ThongTinThanhVien1.HinhDaiDien = _bmp;
                    }
                }
            }
            catch (Exception)
            {
            }          
        }

        private void dataGrid_ThanhVien_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                lbl_HoTen.Text = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["TenThanhVien"].Value.ToString();
                uS_ThongTinThanhVien1.HoTen = lbl_HoTen.Text;
                uS_ThongTinThanhVien1.NgaySinh = DateTime.Parse(dataGrid_ThanhVien.Rows[e.RowIndex].Cells["NgaySinh"].Value.ToString()).ToShortDateString();
                uS_ThongTinThanhVien1.QueQuan = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["QueQuan"].Value.ToString();
                string _duongdanhinhdaidien = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["DuongDanHinh"].Value.ToString();
                if (_duongdanhinhdaidien != "")
                {
                    Bitmap _bmp = new Bitmap(_duongdanhinhdaidien);
                    if (_bmp != null)
                    {
                        uS_ThongTinThanhVien1.HinhDaiDien = _bmp;
                    }
                }   
            }
            catch (Exception)
            {
            }
            
        }

        private void dataGrid_ThanhVien_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                lbl_HoTen.Text = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["TenThanhVien"].Value.ToString();
                uS_ThongTinThanhVien1.HoTen = lbl_HoTen.Text;
                uS_ThongTinThanhVien1.NgaySinh = DateTime.Parse(dataGrid_ThanhVien.Rows[e.RowIndex].Cells["NgaySinh"].Value.ToString()).ToShortDateString();
                uS_ThongTinThanhVien1.QueQuan = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["QueQuan"].Value.ToString();
                string _duongdanhinhdaidien = dataGrid_ThanhVien.Rows[e.RowIndex].Cells["DuongDanHinh"].Value.ToString();
                if (_duongdanhinhdaidien != "")
                {
                    Bitmap _bmp = new Bitmap(_duongdanhinhdaidien);
                    if (_bmp != null)
                    {
                        uS_ThongTinThanhVien1.HinhDaiDien = _bmp;
                    }
                }
            }
            catch (Exception)
            {
            }  
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            _thanhvien.delete();
        }
        private void LayThanhVien(ref ThanhVienDTO _tv, DataGridViewCellEventArgs e)
        {
            //_tv.MaThanhVien = dataGrid_ThanhVien.Rows[e.RowIndex].Cells[]
        }
    }
}
